/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.Division;
import org.crystalrange.enterprise.service.DivisionService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class DivisionListController extends BaseController {

    protected DivisionService divisionService;
    protected Division selectedDivision;
    protected Listbox listboxDivision;
    protected Search searchDivision;
    protected List<Division> divisions;

    public DivisionService getDivisionService() {
        return this.divisionService;
    }

    public void setDivisionService(DivisionService divisionService) {
        this.divisionService = divisionService;
    }

    public Division getSelectedDivision() {
        return this.selectedDivision;
    }

    public void setSelectedDivision(Division selectedDivision) {
        this.selectedDivision = selectedDivision;
    }

    public List<Division> getDivisions() {
        return this.divisions;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchDivision = new Search(Division.class);
        this.searchDivision.setMaxResults(100);
        this.searchDivision.addFetch("salesOrganization");
        this.searchDivision.addFetch("createdBy");
        this.searchDivision.addFetch("updatedBy");
        this.divisions = this.divisionService.search(this.searchDivision);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemDivisionCreate(Event event) throws Exception {
        this.divisionCreate();
    }

    public void onClick$menuitemDivisionEdit(Event event) throws Exception {
        this.divisionEdit();
    }

    public void onClick$menuitemDivisionDelete(Event event) throws Exception {
        this.divisionDelete();
    }

    public void onClick$menuitemDivisionSearch(Event event) throws Exception {
        this.divisionSearch();
    }

    public void onClick$menuitemDivisionRefresh(Event event) throws Exception {
        this.divisionRefresh();
    }

    public void onClick$toolbarbuttonDivisionCreate(Event event) throws Exception {
        this.divisionCreate();
    }

    public void onClick$toolbarbuttonDivisionEdit(Event event) throws Exception {
        this.divisionEdit();
    }

    public void onClick$toolbarbuttonDivisionDelete(Event event) throws Exception {
        this.divisionDelete();
    }

    public void onClick$toolbarbuttonDivisionSearch(Event event) throws Exception {
        this.divisionSearch();
    }

    public void onClick$toolbarbuttonDivisionRefresh(Event event) throws Exception {
        this.divisionRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.divisionEdit();
    }
    
    private void divisionCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/division.zul");
    }

    private void divisionEdit() throws Exception {
        if (this.selectedDivision != null) {
            this.execution.sendRedirect("~./zul/html/division.zul?Id=" + this.selectedDivision.getId().toString());
        }
    }

    private void divisionDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxDivision.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Division division = (Division) selectedItem.getValue();
                    this.divisionService.remove(division);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.divisions = this.divisionService.search(this.searchDivision);
        this.binder.loadAll();
    }

    private void divisionSearch() throws Exception {
        this.execution.setAttribute("searchDivision", this.searchDivision);

        Window window = (Window) this.execution.createComponents("~./zul/html/divisionSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchDivision = (Search) this.execution.getAttribute("searchDivision");
        }

        this.divisions = this.divisionService.search(this.searchDivision);
        this.binder.loadAll();
    }

    private void divisionRefresh() throws Exception {
        this.divisions = this.divisionService.search(this.searchDivision);
        this.binder.loadAll();
    }
}
